//
//  MineWeatherAddCityViewController.m
//  baodongPro
//
//  Created by feng on 16/3/5.
//  Copyright © 2016年 proper. All rights reserved.
//

#define searchButtonPaddingLR   150
#import "MineWeatherAddCityViewController.h"
#import "WeatherCityModel.h"
#import "MyRequest.h"
@interface MineWeatherAddCityViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,copy)void(^myModelBlock)(id one);
@end
@implementation MineWeatherAddCityViewController
{
    UITextField* searchTF;
    UIScrollView* _bgScrollView;
    UITableView* _historyTableView;
    NSMutableArray* _historyArray;
    

}

/**
 *      @author 冯宝东
 *
 *      初始化实例并且带有一个block 的参数
 *
 *      @param myBlock block 的回调点击了某一个城市
 *
 *      @return 城市的实例
 */
-(instancetype)initWithCityModelBlock:(void(^)(id indexOne))myBlock
{
    self=[super init];
    if (self) {
        
        self.myModelBlock=myBlock;
        
    }


    return self;
}
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.containedView.backgroundColor=[UIColor whiteColor];
    _historyArray=[NSMutableArray array];
    
    NSString*placeHold=@"请输入你想查找的城市名称";
    _bgScrollView=[[UIScrollView alloc] initWithFrame:self.containedView.bounds];
    [self.containedView addSubview:_bgScrollView];
    
    searchTF=[self.view fbd_quickCreateUITextFeildWithFrame:CGRectMake(50, 20, ScreenW-2*50, 30) withPlaceHold:placeHold];
    searchTF.backgroundColor=[UIColor grayColor];
    [_bgScrollView addSubview:searchTF];
    
    UIButton* searchButton=[self.view fbd_quickCreateUIButtonWithFrame:CGRectMake(searchButtonPaddingLR, CGRectGetMaxY(searchTF.frame)+20, ScreenW-2*searchButtonPaddingLR, 50)];
    searchButton.layer.cornerRadius=10;
    [searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(begainSearchCanUsedCitylist) forControlEvents:UIControlEventTouchUpInside];
    [searchButton setBackgroundColor:[UIColor redColor]];
    [_bgScrollView addSubview:searchButton];
    
    CGFloat    originY=CGRectGetMaxY(searchButton.frame)+20;
    CGRect tableCGRect=CGRectMake(0,originY, ScreenW, CGRectGetHeight(self.containedView.frame)-originY);
    
    _historyTableView=[[UITableView alloc] initWithFrame:tableCGRect style:UITableViewStylePlain];
    _historyTableView.delegate=self;
    _historyTableView.dataSource=self;
    [self.containedView addSubview:_historyTableView];

    
    
    

}


-(void)begainSearchCanUsedCitylist
{
    [self.containedView endEditing:YES];
    [searchTF resignFirstResponder];
    
    [self showSVProgressWithTip:@"加载中"];
    NSString* hostUrl=@"http://apis.baidu.com/apistore/weatherservice/citylist";
    [MyRequest setRequestManagerHeade:[NSMutableDictionary dictionaryWithObject:ReportApi_key forKey:@"apikey"]];
    NSMutableDictionary* paramDic=[NSMutableDictionary dictionaryWithObject:searchTF.text forKey:@"cityname"];
    
    [[MyRequest defultTool] getRequestWithURL:hostUrl withParameterDic:paramDic withSuccessed:^(id result) {
        
        
        NSString* tipMSG=[result objectForKey:@"errMsg"];
        NSLog(@"可用的城市列表接口返回：%@",result);
        if ([tipMSG isEqualToString:@"success"]) {
            [self showSVProgressWithTip:@"加载完成"];
            
            NSArray*listArray=[result objectForKey:@"retData"];
            
            [listArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                WeatherCityModel*cityModel=[[WeatherCityModel alloc] initWithDictionary:(NSDictionary*)obj];
                [_historyArray  addObject:cityModel];
                
            }];
            [_historyTableView reloadData];
            
            
        }else
        {
        
            NSLog(@"%@",tipMSG);
            [self showSVProgressWithTip:tipMSG];
        
        }
        
        [self disMissSVProgressWithDelaySecond:2];
        

        
    } withFailedBclock:^(id result) {
        
        [self disMissSVProgress];
        
    } withPerBlock:^(float percentage) {
        
        
        
    }];
















}


#pragma mark ---UITableViewDataSource&&UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _historyArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* indetifier=@"MyCells";
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:indetifier];
    if (!cell)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier ];
        cell.selectionStyle=0;
        
    }
    
    NSInteger row=indexPath.row;
    
    WeatherCityModel* indexModel=    [_historyArray objectAtIndex:row];
    
    cell.textLabel.text=[NSString stringWithFormat:@"%@%@%@%@",indexModel.m_province_cn,indexModel.m_district_cn,indexModel.m_name_cn,indexModel.m_name_en,indexModel.m_area_id];
    
    
//    cell.textLabel.text=@"123456";
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSInteger row= indexPath.row;
    if (self.myModelBlock)
    {
        _myModelBlock([_historyArray objectAtIndex:row]);
    }

    
    [self.navigationController popViewControllerAnimated:YES];

}









@end
